{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Jupyter Notebook使用教程\n",
    ">作者：iJeff\n",
    ">\n",
    ">公众号：Python专栏"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一、运行Anaconda启动Jupyter Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在命令窗口中执行以下命令：\n",
    ">jupyter notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、IPython快捷键"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Jupyter Notebook 有两种键盘输入模式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 命令模式: 键盘输入运行程序命令；这时的单元框线为蓝色。\n",
    "- 编辑模式: 允许你往单元中键入代码或文本；这时的单元框线是绿色的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "# 打印100\n",
    "print(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1、命令模式 （不区分大小写）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\t• Shift + Enter : 运行本单元，选中下个单元\n",
    "    • Ctrl + Enter : 运行本单元\n",
    "    • Alt + Enter : 运行本单元，在其下插入新单元\n",
    "    • Y : 单元转入代码状态\n",
    "    • M : 单元转入markdown状态\n",
    "    • A : 在上方插入新单元\n",
    "    • B : 在下方插入新单元\n",
    "    • DD : 删除选中的单元"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "a  = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2、编辑模式 ( Enter 键启动)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    • Shift + Enter : 运行本单元，选中下一单元  \n",
    "    • Ctrl + Enter : 运行本单元\n",
    "    • Alt + Enter : 运行本单元，在下面插入一单元"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3、其他常用快捷键"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\t• Ctrl + A : 全选\n",
    "\t• Ctrl + Z : 撤销\n",
    "    • Ctrl + C : 复制\n",
    "    • Ctrl + V : 粘贴\n",
    "    • Ctrl + / : 注释或取消注释"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# hello\n",
    "# hello\n",
    "# hello"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、IPython的帮助文档"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 使用help()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过以下命令来获得帮助文档：\n",
    ">help(len)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function len in module builtins:\n",
      "\n",
      "len(obj, /)\n",
      "    Return the number of items in a container.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(len)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 使用?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "或者使用问号：\n",
    ">len?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31mSignature:\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m/\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mDocstring:\u001b[0m Return the number of items in a container.\n",
      "\u001b[1;31mType:\u001b[0m      builtin_function_or_method"
     ]
    }
   ],
   "source": [
    "len?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还可以应用到自定义的变量和自定义的函数上来返回帮助文档\n",
    "\n",
    "此外，使用两个??可以把函数的源代码显示出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add(a, b):\n",
    "    \"\"\"add function\"\"\"\n",
    "    return a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31mSignature:\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mDocstring:\u001b[0m add function\n",
      "\u001b[1;31mFile:\u001b[0m      c:\\users\\administrator\\appdata\\local\\temp\\ipykernel_30420\\2752201843.py\n",
      "\u001b[1;31mType:\u001b[0m      function"
     ]
    }
   ],
   "source": [
    "add?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31mSignature:\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mSource:\u001b[0m   \n",
      "\u001b[1;32mdef\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[1;34m\"\"\"add function\"\"\"\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[1;32mreturn\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mFile:\u001b[0m      c:\\users\\administrator\\appdata\\local\\temp\\ipykernel_30420\\2752201843.py\n",
      "\u001b[1;31mType:\u001b[0m      function"
     ]
    }
   ],
   "source": [
    "add??"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. tab自动补全"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- tab : 代码补全或缩进"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# hello\n",
    "import numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- shift + tab 可以查看函数参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# numpy.array()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 四、IPython魔法命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 运行外部Python文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用下面命令运行外部python文件（默认是当前目录，也可以使用绝对路径）\n",
    "> %run *.py\n",
    "    \n",
    "\n",
    "示例: 在当前目录下有一个myscript.py文件：\n",
    "```\n",
    "def square(x):\n",
    "    \"\"\"square a number\"\"\"\n",
    "    return x ** 2\n",
    "\n",
    "for N in range(1, 4):\n",
    "    print(N, \"squared is\", square(N))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以通过下面命令执行它：\n",
    "> %run myscript.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 squared is 1\n",
      "2 squared is 4\n",
      "3 squared is 9\n"
     ]
    }
   ],
   "source": [
    "%run myscript.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "尤其要注意的是，当我们使用魔法命令执行了一个外部文件时，该文件的函数就能在当前会话中使用\n",
    ">square(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "36"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "square(5)\n",
    "square(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(25, 36)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = square(5)\n",
    "b = square(6)\n",
    "a, b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "36"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = square(5)\n",
    "b = square(6)\n",
    "display(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25 36\n"
     ]
    }
   ],
   "source": [
    "a = square(5)\n",
    "b = square(6)\n",
    "print(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 squared is 1\n",
      "2 squared is 4\n",
      "3 squared is 9\n"
     ]
    }
   ],
   "source": [
    "import myscript"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 运行计时"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "用下面命令计算statement的运行时间：\n",
    "> %time statement\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 0 ns\n",
      "Wall time: 0 ns\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1000000"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# %time ：一般用来统计耗时较长代码的运行时长\n",
    "%time square(1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用下面命令计算statement的平均运行时间：   \n",
    "> %timeit statement\n",
    "\n",
    "timeit会多次运行statement，最后得到一个更为精准的预期运行时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "390 ns ± 8.22 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "# %timeit会多次运行statement，最后得到一个更为精准的预期运行时间\n",
    "%timeit square(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "360 ns ± 3.99 ns per loop (mean ± std. dev. of 3 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit -r 3 -n 1000 square(1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以使用两个百分号来测试多行代码的平均运行时间：\n",
    "\n",
    "`\n",
    "%%timeit\n",
    "\n",
    "statement1\n",
    "\n",
    "statement2\n",
    "\n",
    "statement3\n",
    "\n",
    "`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "524 ns ± 12.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "square(1000)\n",
    "add(10, 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 0 ns\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "square(1000)\n",
    "add(10, 20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "记住：\n",
    "- %time一般用于耗时长的代码段\n",
    "- %timeit一般用于耗时短的代码段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 查看当前会话中的所有变量与函数 （了解）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "快速查看当前会话的所有变量与函数名称：\n",
    ">%who"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "N\t a\t add\t b\t builtins\t debugpy\t file\t ipykernel\t myscript\t \n",
      "square\t \n"
     ]
    }
   ],
   "source": [
    "%who"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看当前会话的所有变量与函数名称的详细信息：\n",
    ">%whos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variable    Type        Data/Info\n",
      "---------------------------------\n",
      "N           int         3\n",
      "a           int         25\n",
      "add         function    <function add at 0x00000170910BED40>\n",
      "b           int         36\n",
      "builtins    module      <module 'builtins' (built-in)>\n",
      "debugpy     module      <module 'debugpy' from 'd<...>s\\\\debugpy\\\\__init__.py'>\n",
      "file        str         C:\\Users\\Administrator\\Ap<...>ernel_30420\\1385013460.py\n",
      "ipykernel   module      <module 'ipykernel' from <...>\\ipykernel\\\\__init__.py'>\n",
      "myscript    module      <module 'myscript' from '<...>upyter教程代码\\\\myscript.py'>\n",
      "square      function    <function square at 0x00000170910BF250>\n"
     ]
    }
   ],
   "source": [
    "%whos"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "返回一个字符串列表，里面元素是当前会话的所有变量与函数名称：\n",
    ">%who_ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['N',\n",
       " 'a',\n",
       " 'add',\n",
       " 'b',\n",
       " 'builtins',\n",
       " 'debugpy',\n",
       " 'file',\n",
       " 'ipykernel',\n",
       " 'myscript',\n",
       " 'square']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%who_ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 安装包"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 使用pip命令安装\n",
    "    - pip install numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Collecting matplotlib\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/09/6c/0fa50c001340a45cde44853c116d6551aea741e59a7261c38f473b53553b/matplotlib-3.9.2-cp310-cp310-win_amd64.whl (7.8 MB)\n",
      "     ---------------------------------------- 0.0/7.8 MB ? eta -:--:--\n",
      "     ------------ --------------------------- 2.4/7.8 MB 12.2 MB/s eta 0:00:01\n",
      "     ------------------------ --------------- 4.7/7.8 MB 11.9 MB/s eta 0:00:01\n",
      "     ------------------------------------- -- 7.3/7.8 MB 11.9 MB/s eta 0:00:01\n",
      "     ---------------------------------------- 7.8/7.8 MB 11.5 MB/s eta 0:00:00\n",
      "Collecting contourpy>=1.0.1 (from matplotlib)\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b6/b2/27c7a0d46c7dceb9083272eb314bef1ed43e5280a4197719656f866b496d/contourpy-1.2.1-cp310-cp310-win_amd64.whl (187 kB)\n",
      "Collecting cycler>=0.10 (from matplotlib)\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e7/05/c19819d5e3d95294a6f5947fb9b9629efb316b96de511b418c53d245aae6/cycler-0.12.1-py3-none-any.whl (8.3 kB)\n",
      "Collecting fonttools>=4.22.0 (from matplotlib)\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/70/11/7b81b12a5614b5d237ab70c38bdc268de3eb3880ce7bb1269122e0a415ea/fonttools-4.53.1-cp310-cp310-win_amd64.whl (2.2 MB)\n",
      "     ---------------------------------------- 0.0/2.2 MB ? eta -:--:--\n",
      "     ---------------------------------------- 2.2/2.2 MB 11.3 MB/s eta 0:00:00\n",
      "Collecting kiwisolver>=1.3.1 (from matplotlib)\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4a/a1/8a9c9be45c642fa12954855d8b3a02d9fd8551165a558835a19508fec2e6/kiwisolver-1.4.5-cp310-cp310-win_amd64.whl (56 kB)\n",
      "Requirement already satisfied: numpy>=1.23 in d:\\developer\\wgj\\vscode_workspace\\python\\pyecharts\\.venv\\lib\\site-packages (from matplotlib) (2.0.1)\n",
      "Requirement already satisfied: packaging>=20.0 in d:\\developer\\wgj\\vscode_workspace\\python\\pyecharts\\.venv\\lib\\site-packages (from matplotlib) (24.1)\n",
      "Collecting pillow>=8 (from matplotlib)\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f4/72/0203e94a91ddb4a9d5238434ae6c1ca10e610e8487036132ea9bf806ca2a/pillow-10.4.0-cp310-cp310-win_amd64.whl (2.6 MB)\n",
      "     ---------------------------------------- 0.0/2.6 MB ? eta -:--:--\n",
      "     ------------------------------------ --- 2.4/2.6 MB 12.2 MB/s eta 0:00:01\n",
      "     ---------------------------------------- 2.6/2.6 MB 11.3 MB/s eta 0:00:00\n",
      "Collecting pyparsing>=2.3.1 (from matplotlib)\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9d/ea/6d76df31432a0e6fdf81681a895f009a4bb47b3c39036db3e1b528191d52/pyparsing-3.1.2-py3-none-any.whl (103 kB)\n",
      "Requirement already satisfied: python-dateutil>=2.7 in d:\\developer\\wgj\\vscode_workspace\\python\\pyecharts\\.venv\\lib\\site-packages (from matplotlib) (2.9.0.post0)\n",
      "Requirement already satisfied: six>=1.5 in d:\\developer\\wgj\\vscode_workspace\\python\\pyecharts\\.venv\\lib\\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)\n",
      "Installing collected packages: pyparsing, pillow, kiwisolver, fonttools, cycler, contourpy, matplotlib\n",
      "Successfully installed contourpy-1.2.1 cycler-0.12.1 fonttools-4.53.1 kiwisolver-1.4.5 matplotlib-3.9.2 pillow-10.4.0 pyparsing-3.1.2\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5. 更多魔法命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列出所有魔法命令\n",
    ">lsmagic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看魔法命令的文档:\n",
    "使用?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "application/json": {
       "cell": {
        "!": "OSMagics",
        "HTML": "Other",
        "SVG": "Other",
        "bash": "Other",
        "capture": "ExecutionMagics",
        "cmd": "Other",
        "code_wrap": "ExecutionMagics",
        "debug": "ExecutionMagics",
        "file": "Other",
        "html": "DisplayMagics",
        "javascript": "DisplayMagics",
        "js": "DisplayMagics",
        "latex": "DisplayMagics",
        "markdown": "DisplayMagics",
        "perl": "Other",
        "prun": "ExecutionMagics",
        "pypy": "Other",
        "python": "Other",
        "python2": "Other",
        "python3": "Other",
        "ruby": "Other",
        "script": "ScriptMagics",
        "sh": "Other",
        "svg": "DisplayMagics",
        "sx": "OSMagics",
        "system": "OSMagics",
        "time": "ExecutionMagics",
        "timeit": "ExecutionMagics",
        "writefile": "OSMagics"
       },
       "line": {
        "alias": "OSMagics",
        "alias_magic": "BasicMagics",
        "autoawait": "AsyncMagics",
        "autocall": "AutoMagics",
        "automagic": "AutoMagics",
        "autosave": "KernelMagics",
        "bookmark": "OSMagics",
        "cd": "OSMagics",
        "clear": "KernelMagics",
        "cls": "KernelMagics",
        "code_wrap": "ExecutionMagics",
        "colors": "BasicMagics",
        "conda": "PackagingMagics",
        "config": "ConfigMagics",
        "connect_info": "KernelMagics",
        "copy": "Other",
        "ddir": "Other",
        "debug": "ExecutionMagics",
        "dhist": "OSMagics",
        "dirs": "OSMagics",
        "doctest_mode": "BasicMagics",
        "echo": "Other",
        "ed": "Other",
        "edit": "KernelMagics",
        "env": "OSMagics",
        "gui": "BasicMagics",
        "hist": "Other",
        "history": "HistoryMagics",
        "killbgscripts": "ScriptMagics",
        "ldir": "Other",
        "less": "KernelMagics",
        "load": "CodeMagics",
        "load_ext": "ExtensionMagics",
        "loadpy": "CodeMagics",
        "logoff": "LoggingMagics",
        "logon": "LoggingMagics",
        "logstart": "LoggingMagics",
        "logstate": "LoggingMagics",
        "logstop": "LoggingMagics",
        "ls": "Other",
        "lsmagic": "BasicMagics",
        "macro": "ExecutionMagics",
        "magic": "BasicMagics",
        "mamba": "PackagingMagics",
        "matplotlib": "PylabMagics",
        "micromamba": "PackagingMagics",
        "mkdir": "Other",
        "more": "KernelMagics",
        "notebook": "BasicMagics",
        "page": "BasicMagics",
        "pastebin": "CodeMagics",
        "pdb": "ExecutionMagics",
        "pdef": "NamespaceMagics",
        "pdoc": "NamespaceMagics",
        "pfile": "NamespaceMagics",
        "pinfo": "NamespaceMagics",
        "pinfo2": "NamespaceMagics",
        "pip": "PackagingMagics",
        "popd": "OSMagics",
        "pprint": "BasicMagics",
        "precision": "BasicMagics",
        "prun": "ExecutionMagics",
        "psearch": "NamespaceMagics",
        "psource": "NamespaceMagics",
        "pushd": "OSMagics",
        "pwd": "OSMagics",
        "pycat": "OSMagics",
        "pylab": "PylabMagics",
        "qtconsole": "KernelMagics",
        "quickref": "BasicMagics",
        "recall": "HistoryMagics",
        "rehashx": "OSMagics",
        "reload_ext": "ExtensionMagics",
        "ren": "Other",
        "rep": "Other",
        "rerun": "HistoryMagics",
        "reset": "NamespaceMagics",
        "reset_selective": "NamespaceMagics",
        "rmdir": "Other",
        "run": "ExecutionMagics",
        "save": "CodeMagics",
        "sc": "OSMagics",
        "set_env": "OSMagics",
        "store": "StoreMagics",
        "sx": "OSMagics",
        "system": "OSMagics",
        "tb": "ExecutionMagics",
        "time": "ExecutionMagics",
        "timeit": "ExecutionMagics",
        "unalias": "OSMagics",
        "unload_ext": "ExtensionMagics",
        "who": "NamespaceMagics",
        "who_ls": "NamespaceMagics",
        "whos": "NamespaceMagics",
        "xdel": "NamespaceMagics",
        "xmode": "BasicMagics"
       }
      },
      "text/plain": [
       "Available line magics:\n",
       "%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %code_wrap  %colors  %conda  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %mamba  %matplotlib  %micromamba  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode\n",
       "\n",
       "Available cell magics:\n",
       "%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%code_wrap  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile\n",
       "\n",
       "Automagic is ON, % prefix IS NOT needed for line magics."
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lsmagic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31mDocstring:\u001b[0m\n",
      "Run the named file inside IPython as a program.\n",
      "\n",
      "Usage::\n",
      "\n",
      "  %run [-n -i -e -G]\n",
      "       [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]\n",
      "       ( -m mod | filename ) [args]\n",
      "\n",
      "The filename argument should be either a pure Python script (with\n",
      "extension ``.py``), or a file with custom IPython syntax (such as\n",
      "magics). If the latter, the file can be either a script with ``.ipy``\n",
      "extension, or a Jupyter notebook with ``.ipynb`` extension. When running\n",
      "a Jupyter notebook, the output from print statements and other\n",
      "displayed objects will appear in the terminal (even matplotlib figures\n",
      "will open, if a terminal-compliant backend is being used). Note that,\n",
      "at the system command line, the ``jupyter run`` command offers similar\n",
      "functionality for executing notebooks (albeit currently with some\n",
      "differences in supported options).\n",
      "\n",
      "Parameters after the filename are passed as command-line arguments to\n",
      "the program (put in sys.argv). Then, control returns to IPython's\n",
      "prompt.\n",
      "\n",
      "This is similar to running at a system prompt ``python file args``,\n",
      "but with the advantage of giving you IPython's tracebacks, and of\n",
      "loading all variables into your interactive namespace for further use\n",
      "(unless -p is used, see below).\n",
      "\n",
      "The file is executed in a namespace initially consisting only of\n",
      "``__name__=='__main__'`` and sys.argv constructed as indicated. It thus\n",
      "sees its environment as if it were being run as a stand-alone program\n",
      "(except for sharing global objects such as previously imported\n",
      "modules). But after execution, the IPython interactive namespace gets\n",
      "updated with all variables defined in the program (except for __name__\n",
      "and sys.argv). This allows for very convenient loading of code for\n",
      "interactive work, while giving each program a 'clean sheet' to run in.\n",
      "\n",
      "Arguments are expanded using shell-like glob match.  Patterns\n",
      "'*', '?', '[seq]' and '[!seq]' can be used.  Additionally,\n",
      "tilde '~' will be expanded into user's home directory.  Unlike\n",
      "real shells, quotation does not suppress expansions.  Use\n",
      "*two* back slashes (e.g. ``\\\\*``) to suppress expansions.\n",
      "To completely disable these expansions, you can use -G flag.\n",
      "\n",
      "On Windows systems, the use of single quotes `'` when specifying\n",
      "a file is not supported. Use double quotes `\"`.\n",
      "\n",
      "Options:\n",
      "\n",
      "-n\n",
      "  __name__ is NOT set to '__main__', but to the running file's name\n",
      "  without extension (as python does under import).  This allows running\n",
      "  scripts and reloading the definitions in them without calling code\n",
      "  protected by an ``if __name__ == \"__main__\"`` clause.\n",
      "\n",
      "-i\n",
      "  run the file in IPython's namespace instead of an empty one. This\n",
      "  is useful if you are experimenting with code written in a text editor\n",
      "  which depends on variables defined interactively.\n",
      "\n",
      "-e\n",
      "  ignore sys.exit() calls or SystemExit exceptions in the script\n",
      "  being run.  This is particularly useful if IPython is being used to\n",
      "  run unittests, which always exit with a sys.exit() call.  In such\n",
      "  cases you are interested in the output of the test results, not in\n",
      "  seeing a traceback of the unittest module.\n",
      "\n",
      "-t\n",
      "  print timing information at the end of the run.  IPython will give\n",
      "  you an estimated CPU time consumption for your script, which under\n",
      "  Unix uses the resource module to avoid the wraparound problems of\n",
      "  time.clock().  Under Unix, an estimate of time spent on system tasks\n",
      "  is also given (for Windows platforms this is reported as 0.0).\n",
      "\n",
      "If -t is given, an additional ``-N<N>`` option can be given, where <N>\n",
      "must be an integer indicating how many times you want the script to\n",
      "run.  The final timing report will include total and per run results.\n",
      "\n",
      "For example (testing the script uniq_stable.py)::\n",
      "\n",
      "    In [1]: run -t uniq_stable\n",
      "\n",
      "    IPython CPU timings (estimated):\n",
      "      User  :    0.19597 s.\n",
      "      System:        0.0 s.\n",
      "\n",
      "    In [2]: run -t -N5 uniq_stable\n",
      "\n",
      "    IPython CPU timings (estimated):\n",
      "    Total runs performed: 5\n",
      "      Times :      Total       Per run\n",
      "      User  :   0.910862 s,  0.1821724 s.\n",
      "      System:        0.0 s,        0.0 s.\n",
      "\n",
      "-d\n",
      "  run your program under the control of pdb, the Python debugger.\n",
      "  This allows you to execute your program step by step, watch variables,\n",
      "  etc.  Internally, what IPython does is similar to calling::\n",
      "\n",
      "      pdb.run('execfile(\"YOURFILENAME\")')\n",
      "\n",
      "  with a breakpoint set on line 1 of your file.  You can change the line\n",
      "  number for this automatic breakpoint to be <N> by using the -bN option\n",
      "  (where N must be an integer). For example::\n",
      "\n",
      "      %run -d -b40 myscript\n",
      "\n",
      "  will set the first breakpoint at line 40 in myscript.py.  Note that\n",
      "  the first breakpoint must be set on a line which actually does\n",
      "  something (not a comment or docstring) for it to stop execution.\n",
      "\n",
      "  Or you can specify a breakpoint in a different file::\n",
      "\n",
      "      %run -d -b myotherfile.py:20 myscript\n",
      "\n",
      "  When the pdb debugger starts, you will see a (Pdb) prompt.  You must\n",
      "  first enter 'c' (without quotes) to start execution up to the first\n",
      "  breakpoint.\n",
      "\n",
      "  Entering 'help' gives information about the use of the debugger.  You\n",
      "  can easily see pdb's full documentation with \"import pdb;pdb.help()\"\n",
      "  at a prompt.\n",
      "\n",
      "-p\n",
      "  run program under the control of the Python profiler module (which\n",
      "  prints a detailed report of execution times, function calls, etc).\n",
      "\n",
      "  You can pass other options after -p which affect the behavior of the\n",
      "  profiler itself. See the docs for %prun for details.\n",
      "\n",
      "  In this mode, the program's variables do NOT propagate back to the\n",
      "  IPython interactive namespace (because they remain in the namespace\n",
      "  where the profiler executes them).\n",
      "\n",
      "  Internally this triggers a call to %prun, see its documentation for\n",
      "  details on the options available specifically for profiling.\n",
      "\n",
      "There is one special usage for which the text above doesn't apply:\n",
      "if the filename ends with .ipy[nb], the file is run as ipython script,\n",
      "just as if the commands were written on IPython prompt.\n",
      "\n",
      "-m\n",
      "  specify module name to load instead of script path. Similar to\n",
      "  the -m option for the python interpreter. Use this option last if you\n",
      "  want to combine with other %run options. Unlike the python interpreter\n",
      "  only source modules are allowed no .pyc or .pyo files.\n",
      "  For example::\n",
      "\n",
      "      %run -m example\n",
      "\n",
      "  will run the example module.\n",
      "\n",
      "-G\n",
      "  disable shell-like glob expansion of arguments.\n",
      "\u001b[1;31mFile:\u001b[0m      d:\\developer\\wgj\\vscode_workspace\\python\\pyecharts\\.venv\\lib\\site-packages\\ipython\\core\\magics\\execution.py"
     ]
    }
   ],
   "source": [
    "%run?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "============================================\n",
    "\n",
    "对应刚使用Jupyter Notebook的同学，需要多加练习，达到数量使用Jupyter写代码\n",
    "\n",
    "============================================"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "练习1： 封装函数求闰年"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_leap(y):\n",
    "    return y%4==0 and y%100!=0 or y%400==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "is_leap(2032)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "练习2：封装函数实现冒泡排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_sort(l):\n",
    "    for i in range(len(l) - 1):\n",
    "        for j in range(len(l) - i - 1):\n",
    "            if l[j] > l[j+1]:\n",
    "                l[j], l[j+1] = l[j+1] , l[j]\n",
    "    return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_sort([1, 3, 6, 5, 2, 8, 7, 4])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
